﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using Catering_Rules;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Text;

namespace Catering.Catering_Page.ShanShiFenXi
{
    public partial class PersonAvgXiaohaoChar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        protected void lbtn_Click(object sender, EventArgs e)
        {
            string CLASSID = DDLTool1.GetDDL_Class();
            string YEARS = DDLTool1.GetYear();
            string AREAID = DDLTool1.GetDDL_Area();
            if (string.IsNullOrEmpty(CLASSID) || string.IsNullOrEmpty(YEARS))
            {
                return;
            }
            View_Monitor vm = new View_Monitor();
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(AREAID, CLASSID, DateTime.Parse(YEARS + "-01-01 00:00:11"), "人均消耗统计");
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetPersonAvgXiaohao(pid, CLASSID, AREAID, YEARS, "", Title);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
            ReportViewer1.Visible = true;
            Session["ddlClassId"] = CLASSID;
            Session["ddlYear"] = YEARS;
            Session["ddlGroupId"] = AREAID;
        }

        protected void lbtnChar_Click(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

            if (Session["ddlClassId"] != null && !string.IsNullOrEmpty(Session["ddlClassId"].ToString()) && Session["ddlYear"] != null && !string.IsNullOrEmpty(Session["ddlYear"].ToString()))
            {
                View_Monitor vm = new View_Monitor();
                string pid = "0";
                if (Session["UserID"] != null) pid = Session["UserID"].ToString();
                string AREAID = Session["ddlGroupId"].ToString();
                string CLASSID = Session["ddlClassId"].ToString();
                string sDTIME = Session["ddlYear"].ToString();
                DateTime DTIME = DateTime.Parse("1949-10-01");
                DateTime.TryParse(sDTIME + "-01-01 00:00:11", out DTIME);

                DataTable dt = vm.GetPersonAvgXiaohao(pid, CLASSID, AREAID, sDTIME, "", "");
                List<Catering_Model.RawMaterialsType> listRaws = new Catering_Rules.RawMaterialsType().GetModelList("").Where(p => p.id != 1 && p.id != 9 && p.id != 5 && p.id != 12 && p.id != 2 && p.id != 10 && p.id != 11).ToList<Catering_Model.RawMaterialsType>();

                // 报表的数据源
                string Title = Bind_Data_to_Controls.Bind_ReportTitle(AREAID, "-10", DTIME, "人均用油对比图");
                Dictionary<string, Dictionary<int, decimal>> list = GetReportModel(listRaws);//(日期列表)
                //Dictionary<string, decimal> Raws = GetRawsModel(listRaws);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int Month = Convert.ToInt32(dt.Rows[i]["Month"]);
                        string TypeName = dt.Rows[i]["TypeName"].ToString();
                        decimal Number = (dt.Rows[i]["Number"] is DBNull || string.IsNullOrEmpty(dt.Rows[i]["Number"].ToString())) ? 0 : Convert.ToDecimal(dt.Rows[i]["Number"]);
                        if (list.ContainsKey(TypeName))
                        {
                            if (list[TypeName].ContainsKey(Month))
                            {
                                list[TypeName][Month] = Number;
                            }
                        }
                    }
                    dt.Dispose();
                }
                string str = "";
                if (list.Count>0)
                {
                    StringBuilder Sresult = new StringBuilder("");
                    foreach (string TypeName in list.Keys)
                    {
                        Sresult.Append("{");
                        Sresult.Append("name:'" + TypeName + "',");
                        Sresult.Append("data: [");
                        string StrData = "";
                        foreach (decimal num in list[TypeName].Values)
                        {
                            StrData += num + ",";
                        }
                        StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                        Sresult.Append(StrData + "]");
                        Sresult.Append("},");
                    }
                    str = Sresult.ToString();
                }
                str = str.Length > 0 ? str.Substring(0, str.Length - 1) : "";

                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "',2," + str + ");", true);
                up1.Visible = true;




                //ReportViewer2.LocalReport.DataSources.Clear();
                //ReportDataSource rds = new ReportDataSource("DataSet1", dt);
                //ReportViewer2.LocalReport.DataSources.Add(rds);
                //ReportViewer2.LocalReport.Refresh();
                //ReportViewer2.Visible = true;
                //up1.Visible = true;
            }
        }

        private Dictionary<int, decimal> GetRawsModel()
        {
            Dictionary<int, decimal> dic = new Dictionary<int, decimal>();
            for (int i = 1; i < 13; i++)
            {

                dic.Add(i, 0);
            }
            return dic;
        }

        private Dictionary<string, Dictionary<int, decimal>> GetReportModel(List<Catering_Model.RawMaterialsType> listRaws)
        {
            Dictionary<string, Dictionary<int, decimal>> result = new Dictionary<string, Dictionary<int, decimal>>();
            foreach (Catering_Model.RawMaterialsType item in listRaws)
            {
                Dictionary<int, decimal> newlist = GetRawsModel();
                result.Add(item.Name, newlist);
            }
            
            return result;
        }
    }
}